using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Xioa_UI.DataOrigin;

namespace Xioa_UI.Controls;

public class XioaCheckBoxGroup : ContentControl
{
    public static readonly DependencyProperty GroupDataProperty =
        ElementBase.Property<XioaCheckBoxGroup, XioaCheckGroupList>("GroupDataProperty");

    public XioaCheckGroupList GroupData
    {
        get => (XioaCheckGroupList)GetValue(GroupDataProperty);
        set => SetValue(GroupDataProperty, value);
    }

    public static readonly DependencyProperty ContentIsHitTestVisibleProperty =
        ElementBase.Property<XioaCheckBoxGroup, bool>("ContentIsHitTestVisibleProperty", false);
    public bool ContentIsHitTestVisible
    {
        get => (bool)GetValue(ContentIsHitTestVisibleProperty);
        set => SetValue(ContentIsHitTestVisibleProperty, value);
    }


    public static readonly DependencyProperty ContentMarginProperty =
        ElementBase.Property<XioaCheckBoxGroup, Thickness>("ContentMarginProperty", new Thickness(10));

    public Thickness ContentMargin
    {
        get => (Thickness)GetValue(ContentMarginProperty);
        set => SetValue(ContentMarginProperty, value);
    }
    public static readonly DependencyProperty CheckColorProperty =
       ElementBase.Property<XioaCheckBoxGroup, Brush>("CheckColorProperty", 
           new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#29B94C")) );

    public Brush CheckColor
    {
        get => (Brush)GetValue(CheckColorProperty);
        set => SetValue(CheckColorProperty, value);
    }
    public XioaCheckBoxGroup()
    {
        this.Loaded += XioaCheckBoxGroup_Loaded;

    }

    private void XioaCheckBoxGroup_Loaded(object sender, RoutedEventArgs e)
    {
        Initialize();
    }

    private void Initialize()
    {
        if (GroupData is null) return;
        var stackpanel = new StackPanel();

        this.AddChild(stackpanel);
        foreach (var t in GroupData.Data)
        {
            XioaCheckBox xioaCheckBox = new XioaCheckBox()
            {
                //Width = 100,
                IsHitTestVisible = this.ContentIsHitTestVisible,
                CheckColor = this.CheckColor,
                FontSize = this.FontSize,
                HorizontalAlignment = this.HorizontalAlignment,
                VerticalAlignment = this.VerticalAlignment,
                Margin = this.ContentMargin
            };
            Binding bindingText = new Binding();
            bindingText.Source = t;
            bindingText.Path = new PropertyPath("Content");
            xioaCheckBox.SetBinding(XioaCheckBox.ContentProperty, bindingText);
            Binding bindingIsCheck = new Binding();
            bindingIsCheck.Source = t;
            bindingIsCheck.Path = new PropertyPath("IsCheck");
            xioaCheckBox.SetBinding(XioaCheckBox.IsCheckedProperty, bindingIsCheck);

            stackpanel.Children.Add(xioaCheckBox);
        }
    }

}